|
|
|
|
|
|
|
purchase a pager, you make a decision whether you want local service, regional service, or nationwide service. This information is then programmed into your pager when you buy it. If you go outside the service area, your pager does not work. In a similar manner, you can declare variables to work in only one procedure, work in any procedure of a form, or work throughout your program. |
|
|
|
|
|
|
|
|
By default, a variable that is implicitly declared is local to the procedure in which it is created. If you don't specify any kind of declaration, explicit or implicit, you create a local variable. Therefore, to create variables that have a scope other than local, you must use a declaration statement. |
|
|
|
|
|
|
|
|
|
The scope of a variable is determined not only by the type of declaration, but also by the location of the declaration. For example, the Dim and Private keywords assume different meanings in different parts of a form's code.  |
|
|
|
|
|
|
|
|
|
|
Creating Variables That Are Available Everywhere |
|
|
|
|
|
|
|
|
In most programs, unless you have only one form and no code modules, you will find that you need some variables that can be accessed from anywhere in the code. These variables are called Public variables. (Other languages, as well as earlier versions of Visual Basic, might refer to them as Global variables. In fact, Visual Basic still recognizes the Global keyword.) These variables are typically used to hold information such as the name of the program's user, or to reference a database that is used throughout the program. They might also be used as flags to indicate various conditions in the program. |
|
|
|
|
|
|
|
|
To create a Public variable, you simply place a declaration statement with the Public keyword in the General Declarations section of a module (.BAS file) of your program. The following line shows the Public declaration of a variable used for storing the program user's name: |
|
|
|
|
|
|
|
|
Public sUserName As String |
|
|
|
|
|
|
|
|
In a form or a class module, the Public keyword has a special meaning. Variables defined as Public act like a property of the form or class that is available anywhere in the program. These properties are referenced like the built-in properties of a form or control instead of like a variable. The Public properties are used to pass information between forms and other parts of your program. For example, suppose the previous declaration for sUserName existed in a form. The following lines of code could be used to set and retrieve the user name from the form: |
|
|
|
|
|
|
|
|
frmLogin.sUserName = "CKRAMER"
frmLogin.Show vbModal
MsgBox "The name entered was " & frmLogin.sUserName |
|
|
|
|
|
|
|
|
The first line of code sets the contents of the public variable (and loads frmLogin if it is not already loaded) as if it were a property of the form. The second line of code shows the form modally, so that the third line of code is not executed until the form is hidden. If a user of the form changes the value of sUserName from within the form itself and then hides the form, the third line of code displays a message box with the new value. |
|
|
|
|
|